package com.ckm.controller;

import com.ckm.annotation.LogInfo;
import com.ckm.entity.ResponseResult;
import com.ckm.entity.Teacher;
import com.ckm.service.TeacherService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Slf4j
@LogInfo
@Validated
@RestController
@RequestMapping("teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 登录接口
     * @param teacher 教师信息
     * @return 成功信息+登录后的用户信息
     */
    @PostMapping("login")
    public ResponseResult login(@RequestBody Teacher teacher) {
        return new ResponseResult(200,"登录成功",teacherService.login(teacher));
    }

    @GetMapping("/info")
    public ResponseResult getTeacher(HttpServletRequest request){
        return new ResponseResult(200,"获取用户信息",teacherService.getTeacher(request.getHeader("token")));
    }

    /**
     * 手动添加教师
     * @param teacherList 教师信息List
     * @return 添加成功信息
     */
    @PostMapping("addTeacher")
    public ResponseResult addTeacher(@RequestBody @Valid  List<Teacher> teacherList) {
        teacherService.addTeacher(teacherList);
        return new ResponseResult(200,"添加成功",null);
    }

    /**
     * 使用excel文件导入
     * @return 成功信息
     */
    @PostMapping("addBatch")
    public ResponseResult addBatchTeacher() {
        return new ResponseResult(200,"添加成功",null);
    }

    /**
     * 更新教师
     * @param teacher 更新的信息
     * @return 更新成功信息
     */
    @PutMapping
    public ResponseResult updateTeacher(@RequestBody Teacher teacher) {
        teacherService.updateTeacher(teacher);
        return new ResponseResult(200,"更新成功","");
    }

    /**
     * 删除教师
     * @param id 教师Id
     * @return 删除成功信息
     */
    @DeleteMapping
    public ResponseResult deleteTeacher(@RequestParam(name = "id") Integer id) {
        teacherService.deleteTeacher(id);
        return new ResponseResult(200,"删除成功","");
    }

    /**
     * 查找教师信息
     * @param teacher 查找词
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return 分页结果集
     */
    @PostMapping("find")
    public ResponseResult findTeacher(@RequestBody Teacher teacher,@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,@RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        return new ResponseResult(200,"查找完毕",teacherService.getTeacherPage(teacher,pageNum,pageSize));
    }
}
